#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <SDL/SDL.h>
#include "personnage.h"
#include "interaction_P_P.h"


void Attaquer (_personnage* Perso1, _personnage* Perso2){
if ( Get_Attk(Perso1) == 1 ){

Set_Attk(Perso1,0);
Set_Pv(Perso2, Get_Pv(Perso2)-Get_Deg(Perso1));

if (Get_Pv(Perso2) < 0)
	Set_Pv(Perso2,0);
}
}








int Est_Accesible(const _personnage* Perso1,const _personnage* Perso2, _terrain* Terrain){

int Step;
int X,Y; //compteurs
float A,Fy,Fx;
int Perso1Posx;
int Perso1Posy;
int Perso2Posx;
int Perso2Posy;

Perso1Posx = Get_Posx(Perso1);
Perso1Posy = Get_Posy(Perso1);
Perso2Posx = Get_Posx(Perso2);
Perso2Posy = Get_Posx(Perso2);




if ( Perso1Posx > Perso2Posx+PO1 )
	return 0; 

if ( Perso1Posx < Perso2Posx-PO1 )
	return 0;

if ( Perso1Posy > Perso2Posy+PO1 )
	return 0; 

if ( Perso1Posy < Perso2Posy-PO1 )
	return 0;
// Cases trop éloignées gérées

if (Perso1Posx == Perso2Posx+1)
	return 1;

if (Perso1Posx == Perso2Posx-1)
	return 1;

if (Perso1Posy == Perso2Posy+1)
	return 1;

if (Perso1Posy == Perso2Posy-1)
	return 1;
// Cases adjacente gérées 


if ( Perso1->Type == 1 ) {

if ( (abs(Perso1Posx-Perso2Posx) <= 1) && (abs(Perso1Posy-Perso2Posy) <= 1) ){
return 1;
}


if ( abs(Perso1Posx-Perso2Posx) > abs(Perso1Posy-Perso2Posy) ){
	A = (float)(Perso2Posy-Perso1Posy)/(float)(Perso2Posx-Perso1Posx);
	Fy = (float)Perso1Posy;
	if ( Perso2Posx > Perso1Posx )
		Step = 1;
	if ( Perso2Posx > Perso1Posx )
		Step = -1;
	for ( X = Perso1Posx; X!=Perso2Posx; X+=Step){
		if ( ( Terrain->Cases[X][(int)floor(Fy)] == 1 ) || ( Terrain->Cases[X][(int)ceil(Fy)] == 1 ) )
			return 0;
		else
			return 1;
	}
}
else {
	A = (float)(Perso2Posy-Perso1Posy)/(float)(Perso2Posx-Perso1Posx);
	Fx = (float)Perso1Posx;
	if ( Perso2Posy > Perso1Posy )
		Step = 1;
	if ( Perso2Posy > Perso1Posy )
		Step = -1;
	for ( Y = Perso1Posy; Y!=Perso2Posy; Y+=Step){
		if ( ( Terrain->Cases[(int)floor(Fx)][Y] == 1 ) || ( Terrain->Cases[(int)ceil(Fx)][Y] == 1 ) )
			return 0;
		else 
			return 1;
	}



}
}







}

